﻿using KAsFrame.Application.Contracts;
using KAsFrame.Common.Application;
using KAsFrame.Common.Dto;
using KAsFrame.Domain.School.Dto;
using KAsFrame.Domain.School.Entities;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace KAsFrame.Application
{
    public class StudentAppService : CommonCurdAppService<int, Student, StudentCreateDto, StudentCreateDto, StudentDto>, IStudentAppService
    {
        // 如果需要使用其他服务可以在这里定义私有可读变量，并在构造函数中注入

        public StudentAppService(ISqlSugarClient db) : base(db)
        {

        }

        // 增删改可重写
        public override Task<long> CreateAsync(StudentCreateDto createDto)
        {
            //....
            return base.CreateAsync(createDto);
        }

        // 分页请求必须实现
        public override async Task<PageReturnDto<StudentDto>> GetPageListAsync(PageReqDto pageReqDto)
        {
            Expressionable<Student> exp = Expressionable.Create<Student>();
            if (pageReqDto.Condition.Count > 0)
            {
                var condition = pageReqDto.Condition;

                var nameKey = condition.Keys.FirstOrDefault(x => x.Contains("name", StringComparison.OrdinalIgnoreCase));
                if (nameKey != null)
                    exp = exp.And(x => x.Name.Contains(condition[nameKey].ToString()));
            }

            return await PageAsync(pageReqDto.PageIndex, pageReqDto.PageSize, exp, pageReqDto.OrderBy);
        }

        // 其他业务，需要接口定义
        public async Task<bool> TestAsync()
        {
            return true;
        }
    }
}
